package cn.jji8.patternLogin.spigot.data;

import cn.jja8.patronSaint_2021_7_8_2056.allUsed.data.string.StringDataList;
import cn.jja8.patronSaint_2021_7_8_2056.allUsed.file.ReadFile;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.*;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class localData implements DataMinger{
    //数据文件的保存位置,密码和下线是位子
    File passWordFile = new File(Data.plugin.getDataFolder(),"data/password");
    //ip保存位置
    File playerIpFile = new File(Data.plugin.getDataFolder(),"data/playerIp");
    public localData(){
        playerIpFile.mkdirs();
        passWordFile.mkdirs();
    }
    /**
     * 获取玩家是否需要登入
     * */
    public boolean getPlayerNeedLogin(String playerName){
        return true;
    }
    /**
     * 设置玩家已经登录
     * */
    public void setPlayerAlreadyLogin(String playerName){
    }
    /**
     * 设置某玩家的密码数据
     * */
    public void setPassword(String playerName, String password){
            //自己写文件
            File playerDataFile = new File(passWordFile,playerName+"yml");
            try {
                //noinspection ResultOfMethodCallIgnored
                playerDataFile.createNewFile();
                FileWriter fileWriter = new FileWriter(playerDataFile);
                fileWriter.write(password);
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
                Data.plugin.getLogger().severe("将玩家数据写入"+playerDataFile+"失败！");
            }
    }
    /**
     * 获取某玩家的密码数据
     * */
    public String getPassword(String playerName){
            //自己读文件
            File playerDataFile = new File(passWordFile,playerName+"yml");
            if(!playerDataFile.exists()){
                return null;
            }
            try {
                FileReader fileReader = new FileReader(playerDataFile);
                StringBuilder stringBuilder = new StringBuilder();
                while (true){
                    try {
                        int i = fileReader.read();
                        if(i==-1){
                            break;
                        }
                        stringBuilder.append((char)i);
                    } catch (IOException e) {
                        e.printStackTrace();
                        Data.plugin.getLogger().severe("取文件"+playerDataFile.toString()+"时，意外中断！");
                        return "sa3df4s6ad415";
                    }
                }
                try {
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                    Data.plugin.getLogger().severe("关闭文件"+playerDataFile.toString()+"时，出现意外！");
                }
                String password =stringBuilder.toString();
                if(password.length()<5){
                    return null;
                }
                return password;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                Data.plugin.getLogger().severe("无法读取文件"+playerDataFile.toString());
                return "sa3df4s6ad415";
            }

    }
    /**
     * 设置玩家下线时位置
     * */
    public void setLocation(String playerName, Location location){
        {
            //自己写文件
            File playerDataFile = new File(Data.offlineLocationFile,playerName+"yml");
            YamlConfiguration yamlConfiguration = new YamlConfiguration();
            yamlConfiguration.set("w", Objects.requireNonNull(location.getWorld()).getName());
            yamlConfiguration.set("x",location.getX());
            yamlConfiguration.set("y",location.getY());
            yamlConfiguration.set("z",location.getZ());
            yamlConfiguration.set("pa",(double)location.getPitch());
            yamlConfiguration.set("ya",(double)location.getYaw());
            try {
                yamlConfiguration.save(playerDataFile);
            } catch (IOException e) {
                e.printStackTrace();
                Data.plugin.getLogger().severe("保存"+playerDataFile+"出错！");
            }
        }
    }
    /**
     * 获取玩家下线时位置
     * */
    public Location getLocation(String playerName){
        {
            //自己读文件
            File playerDataFile = new File(Data.offlineLocationFile,playerName+"yml");
            if(!playerDataFile.exists()){
                return null;
            }
            YamlConfiguration yamlConfiguration = new YamlConfiguration();
            try {
                yamlConfiguration.load(playerDataFile);
                String w = yamlConfiguration.getString("w");
                double x = yamlConfiguration.getDouble("x");
                double y = yamlConfiguration.getDouble("y");
                double z = yamlConfiguration.getDouble("z");
                double pa = yamlConfiguration.getDouble("p");
                double ya = yamlConfiguration.getDouble("y");
                if(w==null){
                    return null;
                }
                World world = Bukkit.getServer().getWorld(w);
                if(world==null){
                    Data.plugin.getLogger().severe(playerName+"玩家下线时的"+w+"世界已经不存在？，无法获取上传下线时的位置。");
                    return null;
                }
                return new Location(world,x,y,z,(float)ya,(float)pa);
            } catch (IOException | InvalidConfigurationException e) {
                e.printStackTrace();
                Data.plugin.getLogger().severe("加载文借"+playerDataFile+"数据出错！");
                return null;
            }
        }
    }
    //---------------------------------------------------------------------
    /**
     * 获取某ip下的全部玩家
     * */
    public List<String> getAllIpPlayer(InetAddress ip){
        File file = new File(playerIpFile,ip.toString()+".ip.yml");
        try {
            String s = ReadFile.读取为字符串(file);
            return StringDataList.loadStringList(s);
        } catch (IOException e) {
            e.printStackTrace();
            Data.plugin.getLogger().severe("加载文借"+file+"数据出错！");
            return new ArrayList<>();
        }
    }
    /**
     * 设置玩家在某个ip下
     * */
    public void setPlayerIp(String playerName, InetAddress ip){
        File file = new File(playerIpFile,playerName+".player.yml");
        try {
            String qip = ReadFile.读取为字符串(file);
            ReadFile.写入字符串(ip.toString(),file);
            if (qip.length()<4){
                return;
            }
            File infile = new File(playerIpFile,qip+".ip.yml");
            List<String> list = StringDataList.loadStringList(ReadFile.读取为字符串(infile));
            list.remove(playerName);
            ReadFile.写入字符串(StringDataList.StringListToString(list),infile);
        } catch (IOException e) {
            Data.plugin.getLogger().severe("加载文借数据出错！");
            e.printStackTrace();
        }
    }
}
